package initialize

import (
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/s3"
	"github.com/aws/aws-sdk-go/service/s3/s3manager"
	"server/config"
	"server/global"
)

func NewS3() (s *config.S3, err error) {
	s3Cfg := global.TD27_CONFIG.S3
	var token = ""
	var sess *session.Session
	credential := credentials.NewStaticCredentials(s3Cfg.AccessKey, s3Cfg.SecretKey, token)
	cfg := aws.NewConfig().WithRegion("us-east-1").
		WithEndpoint(s3Cfg.Domain).
		WithCredentials(credential).WithS3ForcePathStyle(true)

	sess, err = session.NewSession(cfg)

	s = &config.S3{
		Svc:        s3.New(sess),
		Uploader:   s3manager.NewUploader(sess),
		Downloader: s3manager.NewDownloader(sess),
	}
	return
}
